package com.tianji.learning.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.api.client.user.UserClient;
import com.tianji.api.dto.user.UserDTO;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.exceptions.DbException;
import com.tianji.common.exceptions.ForbiddenException;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.dto.QuestionFormDTO;
import com.tianji.learning.domain.po.InteractionQuestion;
import com.tianji.learning.domain.query.QuestionPageQuery;
import com.tianji.learning.domain.vo.QuestionVO;
import com.tianji.learning.mapper.InteractionQuestionMapper;
import com.tianji.learning.service.IInteractionQuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

//互动提问的问题表 服务实现类
@Service
@RequiredArgsConstructor
public class InteractionQuestionServiceImpl extends ServiceImpl<InteractionQuestionMapper, InteractionQuestion> implements IInteractionQuestionService {

    private final UserClient userClient;

    @Override
    public void addQuestion(QuestionFormDTO dto) {
        Long userId = UserContext.getUser();

        InteractionQuestion interactionQuestion = BeanUtils.toBean(dto, InteractionQuestion.class);

        interactionQuestion.setUserId(userId);

        baseMapper.insert(interactionQuestion);
    }


    @Override
    public void updateQuestionById(Long id, QuestionFormDTO dto) {
       // InteractionQuestion interactionQuestion = BeanUtils.toBean(dto, InteractionQuestion.class);

        //查询问题是否存在
        InteractionQuestion interactionQuestion  =getById(id);
        if(ObjectUtil.isNotNull(interactionQuestion)){
            throw new ForbiddenException("该问题不存在");
        }

        this.lambdaUpdate()
                .eq(InteractionQuestion::getId, id)
                .update(interactionQuestion);
    }

    //分页查询
    @Override
    public PageDTO<QuestionVO> pageQuestion(QuestionPageQuery questionPageQuery) {
        // 获取提问者昵称
        Long userId = UserContext.getUser();

        UserDTO userDTO = userClient.queryUserById(userId);

        Page<InteractionQuestion> page = questionPageQuery.toMpPage();

        this.lambdaQuery()
                .eq(questionPageQuery.getOnlyMine(), InteractionQuestion::getUserId, userId)
                .eq(ObjectUtil.isNotNull(questionPageQuery.getCourseId()), InteractionQuestion::getCourseId, questionPageQuery.getCourseId())
                .eq(ObjectUtil.isNotNull(questionPageQuery.getSectionId()), InteractionQuestion::getSectionId, questionPageQuery.getSectionId())
                .page(page);

        PageDTO<QuestionVO> pageDTO = PageDTO.of(page, QuestionVO.class);

        for (QuestionVO questionVO : pageDTO.getList()) {
            questionVO.setUserName(userDTO.getName());
            questionVO.setUserIcon(userDTO.getIcon());
        }

        return pageDTO;
    }

    // 根据id查看问题详情
    @Override
    public QuestionVO getQuestionById(Long id) {
        Long userId = UserContext.getUser();

        InteractionQuestion interactionQuestion = this.lambdaQuery()
                .eq(InteractionQuestion::getId, id)
                .one();

        if (ObjectUtil.isNull(interactionQuestion)){
            throw new ForbiddenException("该问题不存在");
        }

        UserDTO userDTO = userClient.queryUserById(userId);

        QuestionVO questionVO = BeanUtils.toBean(interactionQuestion, QuestionVO.class);
        questionVO.setUserName(userDTO.getName());
        questionVO.setUserIcon(userDTO.getIcon());

        return questionVO;
    }

    @Override
    public void deleteById(Long id) {
        // 查询问题是否存在
        InteractionQuestion interactionQuestion = getById(id);
        if(ObjectUtil.isNotNull(interactionQuestion)){
            throw new ForbiddenException("该问题不存在");
        }

        Long userId = UserContext.getUser();
        if(!interactionQuestion.getUserId().equals(userId)){
            throw new ForbiddenException("不能删除其他人的问题");
        }

        int result = baseMapper.deleteById(id);
        if(result == 0){
            throw new DbException("删除失败");
        }

    }

}